package com.example.practice.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.example.practice.R;
import com.example.practice.bean.ChartPagerBean;
import com.example.practice.bean.SensorValue;
import com.example.practice.db.BaseDB.OnDatabaseHandlerListener;
import com.example.practice.db.SensorValueDB;
import com.example.practice.util.Chart;

public class HistoryFragment extends Fragment{
	private Spinner spinnerType;
	private Spinner spinnerTimes;
	private long[] time = new long[]{60000,300000,600000}; 
	private List<ChartPagerBean> chartPagerBeans = new ArrayList<ChartPagerBean>();
	ChartPagerBean soildHumi = new ChartPagerBean("土壤湿度");
	ChartPagerBean soildTemp = new ChartPagerBean("土壤温度");
	ChartPagerBean airHumi = new ChartPagerBean("空气湿度");
	ChartPagerBean airTemp = new ChartPagerBean("空气温度");
	ChartPagerBean co2 = new ChartPagerBean("CO2");
	ChartPagerBean light = new ChartPagerBean("光照");
	private SensorValueDB sensorValueDB;
	private LinearLayout linearLayout;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_history, null);
		linearLayout = (LinearLayout)view.findViewById(R.id.linear);
		sensorValueDB = new SensorValueDB(getActivity());
		
		spinnerType=(Spinner)view.findViewById(R.id.spinner_type);
		spinnerTimes=(Spinner)view.findViewById(R.id.spinner_time);
		addBeans();
		
		
		spinnerType.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				updateChart(chartPagerBeans.get(arg2));
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
		spinnerTimes.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				//填写以下内容
				SensorValueDB sensorValueDB = new SensorValueDB(getActivity());
				sensorValueDB.loadSensorByTime(System.currentTimeMillis() - time[arg2], new OnDatabaseHandlerListener() {
					
					@Override
					public boolean onDatabaseHandler(ArrayList<Object> dataList,
							boolean isSuccess) {
						// TODO Auto-generated method stub
						if(isSuccess){
							for(ChartPagerBean chartPagerBean : chartPagerBeans){
								chartPagerBean.majorValueList.clear();
							}
							
							for(Object object : dataList){
								SensorValue sensorValue = (SensorValue) object;
								if(sensorValue == null)continue;
								chartPagerBeans.get(0).majorValueList.add(sensorValue.getSoilHumid());
								chartPagerBeans.get(1).majorValueList.add(sensorValue.getSoilTemper());
								chartPagerBeans.get(2).majorValueList.add(sensorValue.getAirHumid());
								chartPagerBeans.get(3).majorValueList.add(sensorValue.getAirTemper());
								chartPagerBeans.get(4).majorValueList.add(sensorValue.getCo2());
								chartPagerBeans.get(5).majorValueList.add(sensorValue.getLight());
							}
							
							updateChart(chartPagerBeans.get(spinnerType.getSelectedItemPosition()));
						}
						return false;
					}
				});
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		return view;
	}
	
	private void updateChart(ChartPagerBean sensorDataBeans ){
		new Chart().chart(getActivity(), sensorDataBeans,linearLayout);
	}
	
	private void addBeans(){
		chartPagerBeans.clear();
		chartPagerBeans.add(soildHumi);
		chartPagerBeans.add(soildTemp);
		chartPagerBeans.add(airHumi);
		chartPagerBeans.add(airTemp);
		chartPagerBeans.add(co2);
		chartPagerBeans.add(light);
	}
	
	public void  getHistoryData() {
		
	}
	
}
